package edu.hawaii.ics.pjdb.result_transport;

import java.util.List;

import edu.hawaii.ics.pjdb.types.Tuple;

public abstract class TupleListener {
    private static final TupleListener noopListener = new TupleListener() {
        @Override
        public void receiveTuples(List<Tuple> tuples) {
        }

        @Override
        public void finished() {
        }
    };

    /**
     * 
     * @author chris
     */
    public static abstract class Builder {
        public abstract TupleListener create();

        public LoggingTupleListener.Builder withLogging() {
            return new LoggingTupleListener.Builder(this);
        }
    }

    /**
     * Called when the server receives a list of tuples from a worker.
     * 
     * @param tuples
     */
    public abstract void receiveTuples(List<Tuple> tuples);

    /**
     * Called when the worker reports that it's done sending tuples.
     * 
     */
    public abstract void finished();

    /**
     * A {@link BufferingTupleListener} puts any tuples it receives in an
     * in-memory buffer. It is thread safe.
     * 
     * @param buffer
     * @return
     */
    public static BufferingTupleListener.Builder bufferingListener(
            List<Tuple> buffer) {
        return new BufferingTupleListener.Builder(buffer);
    }

    /**
     * 
     * @return
     */
    public static LoggingTupleListener.Builder loggingListener() {
        return new LoggingTupleListener.Builder();
    }

    /**
     * Returns a tuple listener (not a {@link TupleListener.Builder}) that does
     * nothing.
     * 
     * @return
     */
    public static TupleListener noopListener() {
        return noopListener;
    }
}
